/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.projectcustomconfig.customsourceconfig;

import io.spring.initializr.generator.condition.ConditionalOnLanguage;
import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.language.java.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Modifier;

/**
 * Default Java language contributors.
 *
 * @author Andy Wilkinson
 * @author Stephane Nicoll
 */
@Configuration
class JavaProjectCustomGenerationDefaultContributorsConfiguration {

    @Bean
    @ConditionalOnLanguage(JavaLanguage.ID)
    MethodCustomizer<JavaTypeDeclaration> customMethodContributor() {
        return (typeDeclaration) -> {
            typeDeclaration.modifiers(Modifier.PUBLIC);

            //@RestController
            Annotation restController = Annotation.name("org.springframework.web.bind.annotation.RestController");
            typeDeclaration.annotate(restController);

            // private String WELCOME_MSG = "欢迎消息..."
            JavaFieldDeclaration.Builder welcome_msg = JavaFieldDeclaration
                    .field("WELCOME_MSG")
                    .modifiers(Modifier.PRIVATE)
                    .value("\"欢迎消息\"");
            typeDeclaration.addFieldDeclaration(welcome_msg.returning("java.lang.String"));


            /*
             *   @RequestMapping("/testMsg")
             *   public String testMsg() {
             *      return WELCOME_MSG;
             *   }
             * */
            JavaMethodDeclaration methodDeclaration = JavaMethodDeclaration
                    .method("testMsg")
                    .modifiers(Modifier.PUBLIC).returning("String")
                    .returning("java.lang.String")
                    .body(new JavaReturnStatement(new OneLineExpression("WELCOME_MSG")));
            Annotation requestMapping = Annotation.name("org.springframework.web.bind.annotation.RequestMapping",
                    builder -> builder.attribute("value", String.class, "/testMsg"));
            methodDeclaration.annotate(requestMapping);

            typeDeclaration.addMethodDeclaration(methodDeclaration);
        };
    }


}
